/**
 * related to reg.ui
 * 
 * @Author : wzgl5533
 * @Timestamp : 2017-07-20 注册页面
 */
// 引入组件
var nf = sm("do_Notification");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var do_Global = sm("do_Global");
var do_Network = sm("do_Network");
var dojs = require("dojs");
// 实例化组件
var root = ui("do_ALayout_root");
var userEdt = ui("do_TextField_user");
var validationEdt = ui("do_TextField_validation");
var validationBtn = ui("do_Button_validation");
var pwdEdt = ui("do_TextField_pwd");
var confirmPwdEdt = ui("do_TextField_confirm_pwd");
var regBtn = ui("do_Button_reg");
var xieyiBtn = ui("do_ALayout_xieyi");
// 变量
var dynamicButton = null;// 按钮效果
var initData = null;// 前一个页面携带的数据
var currentOption = null;// 临时存储
var phone = null;//获取验证码的手机号
// 获取初始化配置数据
initData = do_Page.getData();
// 引入标题栏,128px
root.add("titleLy", "source://view/common/topbar.ui", 0, 0);
initView();
// 初始化按钮效果
dynamicButton = "dynamicButton";
dojs.style.css(regBtn, dynamicButton);
dojs.style.css(validationBtn, dynamicButton);
dojs.style.css(xieyiBtn, dynamicButton);
dojs.page.allowHideKeyboard(regBtn);
// 输入焦点处理
var edtFocus = require("source://view/reg/test");
edtFocus.edtFocusEvent(userEdt, ui("do_Label_user"));
edtFocus.edtFocusEvent(validationEdt, ui("do_Label_validation"));
edtFocus.edtFocusEvent(pwdEdt, ui("do_Label_pwd"));
edtFocus.edtFocusEvent(confirmPwdEdt, ui("do_Label_confirm_pwd"));
// --------------------------------------------------------------------------
// 初始化界面
function initView() {

	if (dojs.core.isNullData(initData)) {
		dojs.core.toast("初始化数据为空！");
	} else {
		currentOption = initData;
		// 初始化界面
		userEdt.hint = initData.user.hint;
		validationEdt.hint = initData.validation.hint;
		pwdEdt.hint = initData.password.hint;
		confirmPwdEdt.hint = initData.confirmPwd.hint;
		validationBtn.bgColor = do_Global.getMemory("btn_disable_bg_color");
		validationBtn.enabled = false;
		regBtn.bgColor = do_Global.getMemory("btn_disable_bg_color");
		regBtn.enabled = false;
		

		// 发消息,初始化标题栏
		do_Page.fire("initTopBarMsg", {
			text : dojs.core.isNullData(initData.title) ? "" : initData.title,
			isBack : initData.allowClose
		});
	}
}
// 输入检测--------------------------------------------------------------------
userEdt.on("textChanged", function() {
	if (!dojs.core.isNullData(userEdt.text)) {
		validationBtnStatus("open");
	}else {
		validationBtnStatus("close");
	}
	checkTextChange();
});
validationEdt.on("textChanged", function() {
	checkTextChange();
});
pwdEdt.on("textChanged", function() {
	checkTextChange();
});
confirmPwdEdt.on("textChanged", function() {
	checkTextChange();
});
//订阅按钮状态消息
do_Page.on("validationBtnStatus",function(status){
	validationBtnStatus(status);
});
do_Page.on("regBtnStatus",function(status){
	regBtnStatus(status);
});
do_Page.on("changevalidationText",function(str){
	validationBtn.text = str;
});
//控制按钮使用情况--------------------------------------------------------
function validationBtnStatus(status) {
	if (dojs.core.valueEqual(status, "open")) {
		validationBtn.bgColor = do_Global.getMemory("btn_enable_bg_color");
		validationBtn.enabled = true;
	}else if(dojs.core.valueEqual(status, "close")) {
		validationBtn.bgColor = do_Global.getMemory("btn_disable_bg_color");
		validationBtn.enabled = false;
	}
}
function regBtnStatus(status) {
	if (dojs.core.valueEqual(status, "open")) {
		regBtn.bgColor = do_Global.getMemory("btn_enable_bg_color");
		regBtn.enabled = true;
	}else if(dojs.core.valueEqual(status, "close")) {
		regBtn.bgColor = do_Global.getMemory("btn_disable_bg_color");
		regBtn.enabled = false;
	}
}

//检测信息是否输入完全，显示或禁止登录按钮
function checkTextChange() {
	if (!dojs.core.isNullData(userEdt.text)
			&& !dojs.core.isNullData(validationEdt.text)
			&& !dojs.core.isNullData(pwdEdt.text)
			&& !dojs.core.isNullData(confirmPwdEdt.text)) {
		regBtnStatus("open");
	} else {
		regBtnStatus("close");
	}
}
//点击获取验证码---------------------------------------------------------
dojs.page.onTouch(validationBtn,function(){
	// 判断是否有网络
	if (dojs.core.valueEqual(do_Network.getStatus(), "none")) {
		dojs.core.toast("当前无网络连接，无法登录!");
		return;
	}
	if (dojs.core.isNullData(currentOption.onCallback))
		return;
	
	var regCallback = require(currentOption.onCallback);
	regCallback.invoke({
		type: "obtain_validation",
		phone:userEdt.text,
		time:60
	});
});
//点击注册按钮-----------------------------------------------------------
dojs.page.onTouch(regBtn, function() {
	// 判断是否有网络
	if (dojs.core.valueEqual(do_Network.getStatus(), "none")) {
		dojs.core.toast("当前无网络连接，无法登录!");
		return;
	}
	if (dojs.core.isNullData(currentOption.onCallback))
		return;
	var regCallback = require(currentOption.onCallback);
	var pwd = pwdEdt.text;
	var confirmPwd = confirmPwdEdt.text;
	if (currentOption.password.encryption == "md5") {
		var md5 = require("tools/crypt/md5");
		pwd = md5.hex_md5(pwd).toLowerCase();
		confirmPwd = md5.hex_md5(confirmPwd).toLowerCase();
	}
	
	regCallback.invoke({
		type: "register",
		phone:userEdt.text,
		validation: validationEdt.text,
		pwd:pwd,
		confirmPwd: confirmPwd,
		pwdLength:pwdEdt.text.trim().length
		
	});
});
dojs.page.onTouch(xieyiBtn,function(){
	do_App.openPage({
		source:"source://view/xieyi/xieyi.ui",
		data:{
			// 标题
			title:"用户协议",
			// 是否允许关闭页面
			allowClose : true
		},
		statusBarState: "show",
		animationType: "push_r2l_1",
		statusBarBgColor: do_Global.getMemory("status_bar_bg_color")
	});
});
// 当前页面下，订阅android系统返回键的事件
do_Page.on("back", function() {
	do_App.closePage();
});